#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: ningbo_select.py
@time: 2018/1/3 19:55
"""
import json
import sys

import time

sys.path.append('..')
sys.path.append('../..')

from common.mongo import MongDb
from logger import Logger

MONGO_DB_SOURCE = {
    'host': '172.16.215.2',
    'port': 40042,
    'db': 'company_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('ningbo_select.log').get_logger()

source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                   MONGO_DB_SOURCE['username'],
                   MONGO_DB_SOURCE['password'], log=log)


def main():
    start_time = time.time()
    target_table_name = 'zhejiang_ningbo_2017'
    source_table_name = 'new_zhejiang_search_list'

    source_db.create_index(target_table_name, [('register_date', MongDb.ASCENDING)])

    result_list = []
    count = 0
    find = 0
    total_num = source_db.select_count(source_table_name)
    for item in source_db.traverse_batch(source_table_name, {}):
        count += 1
        if count % 1000 == 0:
            log.info("当前扫描进度: total = {} find = {} count = {}".format(total_num, find, count))

        register_code = item.get('register_code')
        if register_code is None:
            continue

        register_date = item.get('register_date')
        if register_date is None:
            log.info("当前数据没有注册时间: item = {}".format(json.dumps(item, ensure_ascii=False)))
            continue

        if not register_date.startswith('2017'):
            continue

        if len(register_code) == 18 and register_code[2:6] == '3302':
            result_list.append(item)
            find += 1

        if len(register_code) == 15 and register_code[0:4] == '3302':
            result_list.append(item)
            find += 1

        if len(result_list) >= 1000:
            source_db.insert_batch_data(target_table_name, result_list)
            del result_list[:]

    if len(result_list) > 0:
        source_db.insert_batch_data(target_table_name, result_list)
        del result_list[:]

    log.info("扫描完成:  cost time {} s".format(time.time() - start_time))


if __name__ == '__main__':
    main()
